<template>
  <div class="app-container org">
    <el-dialog
      :class="'el-dialog-theme3'"
      append-to-body
      draggable
      :close-on-click-modal="false"
      v-model="open"
      title="选择耗材用品"
      width="700"
      @close="closedDialog"
    >
      <p class="pinformation">搜索条件</p>
      <el-form
        label-width="80"
        ref="queryRef"
        :model="queryParams"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item label="耗材用品：" prop="consumeCode">
          <el-input
            maxlength="100"
            clearable
            placeholder="请输入名称"
            v-model.trim="queryParams.consumeName"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleQuery">搜索</el-button>
          <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref="mainTableRef"
        :data="outList"
        v-loading="loading"
        height="45vh"
        @select="handleCheckbox"
        :header-cell-class-name="cellClass"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          show-overflow-tooltip
          label="名称"
          align="center"
          prop="consumeName"
        />
        <el-table-column
          show-overflow-tooltip
          label="规格"
          align="center"
          prop="ggType"
        />
          <el-table-column
          show-overflow-tooltip
          label="剩余量"
          align="center"
          prop="remainCount"
          width="90"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
        :layout="layout"
      />
      <template #footer>
        <div class="dialog-footer">
          <span style="margin-right: 15px">已选中 {{ count }} 条</span>
          <el-button type="danger" @click="handleAllRemove" :disabled="!count"
            >清空选中</el-button
          >
          <el-button type="success" @click="handleDefine">确定</el-button>
          <el-button type="danger" @click="open = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { selectedConsumeList,selectedConsume,cancelSelectedConsume,clearAll,outCount } from "@/api/resource/consume";
import bus from "@/utils/eventBus";
const { proxy } = getCurrentInstance();
const queryParams = ref({ pageNum: 1, pageSize: 10 });
const outList = ref([]);
const loading = ref(false);
const open = ref(true);
const total = ref(0);
const layout = ref("total, sizes, prev, pager, next, jumper");
const props = defineProps({
  params: {
    type: Object,
  },
});

function getList() {
  loading.value = true;
  selectedConsumeList(queryParams.value).then((res) => {
    loading.value = false;
    outList.value = res.rows;
    total.value = res.total;
    getCount();
    nextTick(() => {
      outList.value.forEach((item) => {
        if (item.outId) {
          proxy.$refs["mainTableRef"].toggleRowSelection(item, true);
        }
      });
    });
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
handleQuery();
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function openShow(val) {
  open.value = true;
}

function getCount() {
  outCount().then((res) => {
    count.value = res.data;
  });
}

// ---------------------------勾选复选框提交------------------------
const count = ref(0);
function handleCheckbox(selection, row) {
  if (!row.outId) {
    selectedConsume([row.consumeId]).then((res) => {
      getList();
    });
  } else {
    cancelSelectedConsume([row.outId]).then((res) => {
      getList();
    });
  }
}


function handleAllRemove() {
  proxy.$modal.confirm("您当前已经选择了耗材用品，确认要删除吗？").then(() => {
    clearAll().then((res) => {
      proxy.$modal.msgSuccess("删除成功");
      getList();
    });
  });
}

const emit = defineEmits(["closeDialog"]);
function closedDialog() {
  emit("closeDialog");
}
function handleDefine() {
  emit("closeDialog");
  bus.emit("refresh");
}
function cellClass(row) {
  if (row.columnIndex === 0) {
    return "DisableSelection";
  }
}

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

// 记得暴露
defineExpose({
  openShow,
});
</script>

<style>
.el-table__header .DisableSelection .cell {
  visibility: hidden;
}
</style>
